﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Serialization;

namespace JLT.Countdown.Helpers
{
    public class XmlResult : ActionResult
    {
        private object _objectToSerialize;
        private string _xml;

        public XmlResult(object objectToSerialize)
        {
            _objectToSerialize = objectToSerialize;
        }

        public XmlResult(string xml, string dummy)
        {
            _xml = xml;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (_objectToSerialize != null)
            {
                var xs = new XmlSerializer(_objectToSerialize.GetType());
                context.HttpContext.Response.ContentType = "text/xml";
                xs.Serialize(context.HttpContext.Response.Output, _objectToSerialize);
            }
            else
            {
                if (_xml.Length > 0)
                {
                    context.HttpContext.Response.ContentType = "text/xml";
                    context.HttpContext.Response.Output.Write(_xml);
                }

            }
        }
    }
}
